<?php
/**
 * Created by PhpStorm.
 * User: tangmusen
 * Date: 2017/10/30
 * Time: 11:08
 */
namespace app\home\controller;

use app\api\controller\HX;
use think\Controller;
use think\Cookie;
use think\Db;
use think\Request;

class Login extends Controller
{




    /**
     * 登陆界面
     */
    public function login(){

        return view();
    }

    /**
     * 绑定界面
     */
    public function bang(){

        return view();
    }

    /**

     * 绑定操作

     */

    public function login_bang(Request $request){

        $mobile     = $request->post('mobile');

        $code       = $request->post('code');

        $password   = $request->post('password')?$request->post('password'):0;

        $parent_id = Cookie::get('post_tid')? Cookie::get('post_tid'):0;

        if(!$mobile || !$code){
            json_return(204,'缺少参数');
        }

        $Captcha = Db::name('captcha');

        $captcha_info = $Captcha->where(array('mobile'=>$mobile))->find();

        $true_code = $captcha_info['code'];

        if($true_code!=$code){
            json_return(201,'验证码错误');
        }
        $wxUser = session('wxUser');

        $User = Db::name('member');
        $user_info = $User->where(array('mobile'=>$mobile))->find();
        if($user_info){
            $up['nickname']   = $wxUser['nickname'];
            $up['head']       = $wxUser['headimgurl'];
            $up['unionid']    = $wxUser['openid'];
            $re_up =  $User->where(array('mobile'=>$mobile))->update($up);
            if($re_up) {
                json_return(200, '绑定手机成功');
            }
        }
        if($parent_id){
            $re_result = $User->where(array('id'=>$parent_id))->find();
            if($re_result) {
                $data['tid']  = $parent_id;
                $data['level'] = $re_result['level']+1;
            }
        }
        //注册环信
        $Hx = new HX();
        $Hx->openRegister($mobile);
        if($password){
            $data['password']   = md5($password);
        }
        $data['nickname']   = $wxUser['nickname'];
        $data['mobile']     = $mobile;
        $data['create_at']  = time();
        $data['head']       = $wxUser['headimgurl'];
        $data['unionid']    = $wxUser['openid'];
        $data['ip'] = get_client_ip();
        $data['token'] = md5(rand(0000,9999));
        if ($User->insert($data)) {
            $uid = $User->getLastInsID();
            session('uid',$uid);
            $up['gm_name'] = 'ycgm'.$uid;
            $User->where('id',$uid)->update($up);
            json_return(200,'绑定手机成功');
        } else {
            json_return(201,'绑定手机成功');
        }
    }

    /**
     * 登陆
     */

    public function do_login(Request $request){
        $mobile     = $request->post('mobile');
        $password   = $request->post('password');
        if(!$mobile || !$password){
            json_return(204,'缺少参数');
        }
        $User = Db::name('member');
        $user_info =$User
            ->field('id,nickname,head,money,password,token,gm_name')
            ->where(array('mobile'=>$mobile))
            ->find();
        if(!$user_info){
            json_return(201,'用户不存在');
        }
        if(md5($password)!=$user_info['password']){
            json_return(201,'用户名密码错误');
        }else{
            if(!$user_info['gm_name']){
                $data['gm_name'] = 'ycgm'.$user_info['id'];
            }
            $data['token'] = md5(rand(0000,9999));
            $User->where(array('mobile'=>$mobile))->update($data);
            unset($user_info['password']);
            $user_info['token'] = $data['token'];

            $login_log['login_way'] = 3;
            $login_log['uid'] = $user_info['id'];
            $login_log['create_at'] = time();
            $login_log['ip'] = get_client_ip();
            $ip_address = Db::name('login_log')
                ->where(array('ip'=>$login_log['ip'] ))
                ->value('ip_address');
            if(!$ip_address){
                $ip_address =  get_city_id($login_log['ip']);
            }
            $login_log['ip_address'] = $ip_address;
            Db::name('login_log')->insert($login_log);
            session('uid',$user_info['id']);
            json_return(200,'登录成功',$user_info);
        }
    }

    /**
     * 注册界面
     */
    public function register(){
        return view();
    }
    /**
     * 注册界面
     */
    public function logout(){
        session('uid', null);
        $this->redirect('login/login');
    }

    /**
     * 忘记密码
     */
    public function forget_password(){
        return view();
    }

    /**
     *
     * 忘记密码
     */
    public function forget(Request $request){
        $mobile     = $request->post('mobile');

        $code       = $request->post('code');

        $password   = $request->post('password')?$request->post('password'):0;

        if(!$mobile || !$code){
            json_return(204,'缺少参数');
        }
        $Captcha = Db::name('captcha');
        $captcha_info = $Captcha->where(array('mobile'=>$mobile))->find();
        $true_code = $captcha_info['code'];
        if($true_code!=$code){
            json_return(201,'验证码错误');
        }
        $User = Db::name('member');
        $up['password']    = md5($password);
        $re_up =  $User->where(array('mobile'=>$mobile))->update($up);
        if ($re_up) {
            json_return(200,'绑定手机成功');
        } else {
            json_return(201,'绑定手机成功');
        }
    }



}